package com.zzh;

import com.microsoft.playwright.*;
import com.microsoft.playwright.BrowserType.LaunchOptions;
import com.microsoft.playwright.junit.Options;
import com.microsoft.playwright.junit.OptionsFactory;
import com.microsoft.playwright.options.AriaRole;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Objects;

public class CustomOptions implements OptionsFactory {
    @Override
    public Options getOptions() {
        Path path = Paths.get("state.json");
        if (!path.toFile().exists()) {
            // 登录并持久化登录信息
            try (Playwright playwright = Playwright.create()) {
                LaunchOptions launchOptions = new LaunchOptions();
                launchOptions.setHeadless(false);
                Browser browser = playwright.chromium().launch(launchOptions);
                Page page = browser.newPage();
                page.navigate("https://www.ctrip.com");
                page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("登录")).click();
                page.getByTestId("accountNameInput").click();
                // page.getByTestId("accountNameInput").fill("15311450756");
                page.getByTestId("accountNameInput").fill("18116099739");
                page.getByTestId("accountPanel").getByTestId("passwordInput").click();
                page.getByTestId("accountPanel").getByTestId("passwordInput").fill("Zzh.321.321");
                page.locator("label").click();
                page.getByTestId("accountPanel").getByTestId("loginButton").click();
                page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("发送验证码")).click();
                page.getByTestId("doubleAuthSwitcherBox").getByTestId("verifyCodeInput").click();
                page.waitForTimeout(5000);
                // 获取验证码
                String verifyCode = page.getByTestId("doubleAuthSwitcherBox").getByTestId("verifyCodeInput").innerText();
                int countOne = 0;
                while ((Objects.isNull(verifyCode) || verifyCode.length() == 0) && countOne < 3 ) {
                    page.waitForTimeout(5000);
                    verifyCode = page.getByTestId("doubleAuthSwitcherBox").getByTestId("verifyCodeInput").innerText();
                    countOne++;
                }
                // page.getByTestId("doubleAuthSwitcherBox").getByTestId("verifyCodeInput").fill("693411");
                page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("验 证")).click();
                // Save storage state into the file.
                // BrowserContext context = browser.newContext();
                // Page page = context.newPage();
                // context.storageState(new BrowserContext.StorageStateOptions().setPath(path));
                page.context().storageState(new BrowserContext.StorageStateOptions().setPath(path));
                int countTwo = 0;
                while (!path.toFile().exists() && countTwo < 5) {
                    page.waitForTimeout(5000);
                    countTwo++;
                }
            }
        }
        // Create a new context with the saved storage state.
        // BrowserContext context = browser.newContext(new Browser.NewContextOptions().setStorageStatePath(path));
        Browser.NewContextOptions contextOptions = new Browser.NewContextOptions();
        contextOptions
                .setStorageStatePath(path)
                // // 手机模拟
                //.setUserAgent("Mozilla/5.0 (iPad; CPU OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/83.0.4103.88 Mobile/15E148 Safari/604.1")
                //.setViewportSize( 968, 1366)
                //.setDeviceScaleFactor(2)
                //.setBaseURL("https://m.ctrip.com/html5")
                // .setGeolocation(31.2304, 121.4737) // 设置定位 上海
                // 放慢浏览器执行速度，方便查看
                .setGeolocation(39.9042, 116.4074) // 设置定位 北京
                .setPermissions(Arrays.asList("geolocation"))
                .setBaseURL("https://www.ctrip.com");
        LaunchOptions launchOptions = new LaunchOptions()
                .setHeadless(false)
                .setSlowMo(1000);
        return new Options()
                .setLaunchOptions(launchOptions)
                .setContextOptions(contextOptions);
        // .setApiRequestOptions(new APIRequest.NewContextOptions()
        // .setBaseURL("https://playwright.dev"));
    }
}
